/**
 * <p>文件名:		LBSException.java</p>
 * <p>版权:		CopyrightTag</p>
 * <p>公司:		CompanyTag</p>
 * @author		xxx(xxx@xxx.com)
 */

package com.caits.lbs.exception;

import org.apache.log4j.Logger;

import com.caits.lbs.framework.log.CommonLogFactory;

/**
 * <p>
 * LBSException
 * </p>
 * <p>
 * LBS平台所有异常的根，所有业务相关的异常必须继承此类，必须
 * </p>
 * 
 * @author zhb(zhouhuabin@ctfo.com)
 * @version 0.0.0
 *          <table style="border:1px solid gray;">
 *          <tr>
 *          <th width="100px">版本号</th><th width="100px">动作</th><th
 *          width="100px">修改人</th><th width="100px">修改时间</th>
 *          </tr>
 *          <!-- 以 Table 方式书写修改历史 -->
 *          <tr>
 *          <td>0.0.0</td>
 *          <td>创建类</td>
 *          <td>周华彬</td>
 *          <td>2011-2-11 下午03:58:11</td>
 *          </tr>
 *          <tr>
 *          <td>XXX</td>
 *          <td>XXX</td>
 *          <td>XXX</td>
 *          <td>XXX</td>
 *          </tr>
 *          </table>
 */
public class LBSException extends Exception {


	/** 变量:TODO,类型:long */
	private static final long serialVersionUID = -3372722207080299716L;
	protected static Logger log = CommonLogFactory.getLog();
	/** 异常错误码，对于同样的错误码，需要在不同的异常中有不同的表述 */
	private int code;

	/**
	 * 构造器
	 */
	public LBSException() {

	}

	/**
	 * 构造器
	 * @param errorCode	错误代码
	 */
	public LBSException(final int errorCode) {
		this.setCode(errorCode);
	}

	/**
	 * 构造器
	 * 
	 * @param message
	 */
	public LBSException(final String message) {
		super(message);
	}

	/**
	 * 构造器
	 * 
	 * @param code
	 * @param message
	 */
	public LBSException(final int code, final String message) {
		super(message);
		this.setCode(code);
	}

	/**
	 * 构造器
	 * 
	 * @param cause
	 */
	public LBSException(final Throwable cause) {
		super(cause);
	}

	/**
	 * 构造器
	 * 
	 * @param code
	 * @param cause
	 */
	public LBSException(final int code, final Throwable cause) {
		super(cause);
		this.setCode(code);
	}

	/**
	 * 构造器
	 * 
	 * @param message
	 * @param cause
	 */
	public LBSException(final String message, final Throwable cause) {
		super(message, cause);
	}

	/**
	 * 构造器
	 * 
	 * @param code
	 * @param message
	 * @param cause
	 */
	public LBSException(final int code, final String message, final Throwable cause) {
		super(message, cause);
		this.setCode(code);
	}

	/** (non-Javadoc)
	 * @see java.lang.Throwable#toString()
	 */
	public String toString() {
		// 如果输出异常的类信息，会让事情变得复杂并且不可预料，所以还是老老实实输出改输出的吧
		// if (Framework.Debug) {
		// return new JSONObject(this, true).toString();
		// } else {
		// return super.toString();
		// }
		return super.toString();
	}

	/**
	 * 将异常对象输出为字符串
	 * 
	 * @param t	异常
	 * @return	String
	 */
	public static String toStacksString(Throwable t) {
		StringBuilder sb = new StringBuilder("以下是异常（" + t.getMessage()
				+ "）的堆栈信息：\r\n");
		return appendExceptionCause(t, sb).toString();
	}

	/**
	 * FIXME 
	 * @param cause
	 * @param sb
	 * @return StringBuilder
	 */
	private static StringBuilder appendExceptionCause(Throwable cause,
			StringBuilder sb) {
		if (cause == null) {
			return sb;
		}

		if (sb == null) {
			sb = new StringBuilder();
		}

		StackTraceElement[] steArr = cause.getStackTrace();
		int tracesSize = steArr.length;
		for (int i = 0; i < tracesSize; i++) {
			sb.append("     at ");// 在堆栈行开始增加空格
			StackTraceElement trace = steArr[i];
			sb.append(trace.toString() + "\r\n");
		}

		Throwable c = cause.getCause();
		if (c != null) {
			sb.append("\r\n");
			sb.append("Caused by: " + c);
			sb.append(appendExceptionCause(c, sb));
		}

		return sb;
	}

	/**
	 * 获取变量<code>code</code>的值
	 * @return 返回的数据类型是<code>int</code>
	 */
	public int getCode() {
		return code;
	}

	/**
	 * 设置变量<code> code</code> 的值
	 * @param code  <code>code</code> 参数类型是<code>int</code>
	 */
	public void setCode(int code) {
		this.code = code;
	}

	/**
	 * FIXME
	 * 
	 * @param args
	 */
	public static void main(String[] args) {
		try {
			throw new LBSException("gase");
		} catch (LBSException e) {
			log.error("LBSException.main():"
					+ toStacksString(new Exception("hh", e)));
		}
	
	}

}
